<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
var a=[];
var n=prompt('输入数组长度')
for(var i=0.;i<n;i++){
var a1=prompt('请输入数组'+(i+1)+'个元素');
if(a1==''||isNaN(a1)==true)
{n--;
continue;
}
else
{
a.push(parseInt(a1))
}
}
var temp=new Array();
function merge(a,l,m,r,temp)
{
	var i=l,j=m+1;
	var n=r;
	var k=0;
	while(i<=m&&j<=n)
	{
		if(a[i]<a[j])
			temp[k++]=a[i++];
		else
			temp[k++]=a[j++];
	}
	while(i<=m)
		temp[k++]=a[i++];
	while(j<=n)
		temp[k++]=a[j++];
	for(i=0;i<k;i++)
		a[l+i]=temp[i];
}
function mergesort(a,l,r,temp)
{
	if(l<r)
	{
		var m= Math.floor((l+r)/2);
		mergesort(a,l,m,temp);
		mergesort(a,m+1,r,temp);	
		merge(a,l,m,r,temp);	
	}
}
mergesort(a,0,n-1,temp);
alert('排序完成后的数组：'+a);
</script>
</head>
<body>
</body>
</html>
